<template>
  <!-- 历史记录 -->
  <!-- 注意 props中引用类型的数据只能修改，不能重新赋值（否则会造成父组件关联数据无法同步） -->
  <div class="search-history">
    <van-cell title="搜索历史">
      <div v-if="isDeleteShow">
        <!-- //如果isDeleteShow为true显示此模板 -->
        <span @click="$emit('clear-search-histories')">全部删除</span>
        &nbsp;&nbsp;
        <span @click="isDeleteShow = false">完成</span>
        <!-- //点击完成退出删除状态 -->
      </div>
      <!-- //如果isDeleteShow为false，则显示删除按钮。点击删除按钮将isDeleteShow变为true，进入删除状态 -->
      <van-icon v-else name="delete" @click="isDeleteShow = true" />
    </van-cell>
    <van-cell
      :title="item"
      v-for="(item, index) in searchHistories"
      :key="index"
      @click="onSearchItemClick(item, index)"
    >
      <van-icon v-show="isDeleteShow" name="close" />
    </van-cell>
  </div>

  <!-- /历史记录 -->
</template>

<script>
export default {
  name: 'SearchHistory',
  components: {},
  props: {
    searchHistories: {
      type: Array,
      required: true
    }
  },
  data() {
    return {
      // 定义当前是否是删除状态
      isDeleteShow: false // 控制删除显示状态
    }
  },
  computed: {},
  watch: {},
  created() {},
  mounted() {},
  methods: {
    onSearchItemClick(item, index) {
      if (this.isDeleteShow) {
        this.searchHistories.splice(index, 1)
      } else {
        // 否则执行搜索操作
        this.$emit('search', item)
      }
    }
  }
}
</script>

<style scoped lang="less"></style>
